package sv.mecca.modelo;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;





import sv.mecca.form.Clinica1Form;

public class Clinica1Modelo extends Conexion{

	/**
	 * metodo para la clinica1
	 */
	private static final long serialVersionUID = -7039736022972034877L;
	
	public void saveClinica1 (Clinica1Form u) throws SQLException{
		try {
			save("INSERT INTO clinica(id_clinica, id_tipo_clinica, id_paciente, id_usuario, tratamiento, diagnostico) VALUES('"
					+ u.getId_clinica()
					+ "', '"
					+ u.getId_tipo_clinica()
					+ "', "
					+ u.getId_paciente()
					+ ", '"
					+ u.getId_usuario()
					+ "', '"
					+ u.getTratamiento()
					+ "', '"
					+ u.getDiagnostico() + "')");
		} catch (Exception e) {
			System.out.println("Error al guardar la clinica " + e.getMessage());
		}
		
		
	}

	
	/**
	 * Metodo para actualizar un usuario
	 * 
	 * @param u
	 * @throws SQLException
	 */
	
	public void updateClinica1(Clinica1Form u) throws SQLException {
		try {
			update("UPDATE clinicas SET id_tipo_clinica = "
					+ u.getId_tipo_clinica() + ", id_paciente = "
					+ u.getId_paciente() + ", id_usuario = "
					+ u.getId_usuario() + ", tratamiento = '"
					+ u.getTratamiento() + "', diagnostico = '"
					+ u.getDiagnostico() + "' WHERE id_clinica = "
					+ u.getId_clinica());
		} catch (Exception e) {
			System.out.println("Error al actualizar la clinica1 "
					+ e.getMessage());
		}
	}
	
	/**
	 * Metodo para borrar la clinica1
	 * 
	 * @param u
	 * @throws SQLException
	 */
	
	
	public void deleteclinica1(Clinica1Form u) throws SQLException {
		try {
			delete("DELETE FROM clinica WHERE id_clinica = "
					+ u.getId_clinica());
		} catch (Exception e) {
			System.out
					.println("Error al eliminar la clinica1 " + e.getMessage());
		}
	}
	/**
	 * Metodo para listar la clinica
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Clinica1Form> listado() throws SQLException {
		ArrayList<Clinica1Form> c1 = new ArrayList<Clinica1Form>();
		ResultSet rs = executeQueryList("SELECT * FROM clinicas"
				+ " c INNER JOIN tipos_clinicas tu ON c.id_tipo_clinica = tu.id_tipo_clinica"
				+ " inner join pacientes p on c.id_paciente = p.id_paciente"
				+ " inner join usuarios u on c.id_usuario = u.id_usuario");
		while (rs.next()) {
			Clinica1Form t = new Clinica1Form(rs.getInt(1),rs.getInt(2), rs.getString("tipo_clinica"),
					rs.getInt(3), rs.getInt(4), rs.getString("tipo_usuario"), rs.getString(5),
					rs.getString(6));
			c1.add(t);
		}
		return c1;
	}
}
